package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcFlowMessageGroupMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcFlowMessage;
import com.csun.cmny.provider.model.domain.OmcFlowMessageGroup;
import com.csun.cmny.provider.model.dto.message.FlowMessageQueryDto;
import com.csun.cmny.provider.model.vo.message.FlowMessageVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFlowMessageGroupService;
import com.csun.cmny.provider.service.OmcFlowMessageService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-28 10:22
 * @Description:
 */
@Service
@Transactional
public class OmcFlowMessageGroupServiceImpl extends BaseService<OmcFlowMessageGroup>
        implements OmcFlowMessageGroupService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcFlowMessageService omcFlowMessageService;
    @Resource
    private OmcFlowMessageGroupMapper omcFlowMessageGroupMapper;

    /**
     * bind omc flow message with group
     *
     * @param omcFlowMessageGroup the omc message group
     * @return the int
     */
    @Override
    public Integer bind(OmcFlowMessageGroup omcFlowMessageGroup) {

        Long flowMessageId = omcFlowMessageGroup.getFlowMessageId();
        Long groupId = omcFlowMessageGroup.getGroupId();

        Preconditions.checkArgument(PublicUtil.isNotEmpty(flowMessageId),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        Preconditions.checkArgument(PublicUtil.isNotEmpty(groupId),
                cmnyMessageSource.getMessage("group.id.null", null));

        OmcFlowMessage omcFlowMessage = omcFlowMessageService.selectByKey(flowMessageId);
        if (PublicUtil.isEmpty(omcFlowMessage)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031402);
        }

        omcFlowMessageGroup.setMessage(omcFlowMessage.getMessage());
        omcFlowMessageGroup.setParam(omcFlowMessage.getParam());
        omcFlowMessageGroup.setRole(omcFlowMessage.getRole());

        return omcFlowMessageGroupMapper.insertSelective(omcFlowMessageGroup);
    }

    /**
     * untied omc flow message with group
     *
     * @param omcFlowMessageGroup the omc flow message group
     *
     * @return the int
     */
    @Override
    public Integer untied(OmcFlowMessageGroup omcFlowMessageGroup) {

        Long flowMessageId = omcFlowMessageGroup.getFlowMessageId();
        Long groupId = omcFlowMessageGroup.getGroupId();

        Preconditions.checkArgument(PublicUtil.isNotEmpty(flowMessageId),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        Preconditions.checkArgument(PublicUtil.isNotEmpty(groupId),
                cmnyMessageSource.getMessage("group.id.null", null));

        return omcFlowMessageGroupMapper.deleteByPrimaryKey(omcFlowMessageGroup);
    }

    /**
     * edit omc flow message group
     *
     * @param omcFlowMessageGroup the omc flow message group
     * @return the int
     */
    @Override
    public Integer edit(OmcFlowMessageGroup omcFlowMessageGroup) {

        Long flowMessageId = omcFlowMessageGroup.getFlowMessageId();
        Long groupId = omcFlowMessageGroup.getGroupId();

        Preconditions.checkArgument(PublicUtil.isNotEmpty(flowMessageId),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        Preconditions.checkArgument(PublicUtil.isNotEmpty(groupId),
                cmnyMessageSource.getMessage("group.id.null", null));

        return omcFlowMessageGroupMapper.updateByPrimaryKeySelective(omcFlowMessageGroup);
    }

    /**
     * query omc flow message group list with page
     *
     * @param flowMessageQueryDto the flow message query dto
     *
     * @return the list
     */
    @Override
    public List<FlowMessageVo> queryListWithPage(FlowMessageQueryDto flowMessageQueryDto) {

        PageHelper.startPage(flowMessageQueryDto.getPageNum(), flowMessageQueryDto.getPageSize());

        return omcFlowMessageGroupMapper.queryListWithPage(flowMessageQueryDto);
    }

    /**
     * query flow message by code and group id
     *
     * @param code    the code
     * @param groupId the group id
     *
     * @return the flow message vo
     */
    @Override
    public FlowMessageVo queryByCodeAndGroupId(String code, Long groupId) {

        return omcFlowMessageGroupMapper.queryByCodeAndGroupId(code, groupId);
    }
}
